<?php
class Api extends Controller {

	function Api()
	{
            parent::Controller();
            $this->load->model('Location', 'loc');
            $this->load->helper('form');
	}

	function index()
	{
            $this->load->view('api_view');
	}

	//insert waypoint into database(POST)
        function setmarker()
        {
            $lat = $this->input->post('lat');
            $lon = $this->input->post('lon');
            $desc = $this->input->post('desc');
            if(!$lat || !$lon || !$desc ){
                $this->output->set_status_header('400');
            }
            else{
                $this->loc->addPosition($lat, $lon, $desc);
            }
        }

	//return waypoints from database in json format
        function getmarkers()
	{
            $res = $this->loc->getPositions();
	    header('Content-Type: text/plain');
            $loc = json_encode($res);
	    echo $loc;
        }
	
	//return waypoints from database in xml format
	function getmarkersxml()
	{
	   $this->load->helper('xml');
	   $res = $this->loc->getPositions();
	   header('Content-Type: text/xml');
	   $dom = xml_dom();
	   $pos = xml_add_child($dom, 'pos');
	   foreach ($res as $r){
		$way = xml_add_child($pos, 'waypoint');
		xml_add_attribute($way, 'longitude', $r->longitude);
		xml_add_attribute($way, 'latitude', $r->latitude);
		xml_add_attribute($way, 'description', $r->description);
	   }
	   xml_print($dom);
	}
}
?>
